package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogistException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.server.IUserInfoService;
import cn.wolfcode.luowowo.website.anno.RequiredLogin;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@Controller
public class UserInfoController {

    @Reference
    private IUserInfoService service;

    /**
     * 测试
     *
     * @param id
     * @return
     */
    @RequestMapping("/hello")
    @ResponseBody
    @RequiredLogin
    public Object get(Long id) {
        System.out.println("11");
        return service.get(id);
    }

    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone) {
        return !service.checkPhone(phone);
    }

    @ResponseBody
    @RequestMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone) {
        try {
            service.sendVerifyCode(phone);
        } catch (Exception e) {
            return new AjaxResult(false, e.getMessage());
        }
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public Object regist(String verifyCode, String phone, String nickname, String password, String rpassword) {
        try {
            service.insert(verifyCode, phone, nickname, password, rpassword);
        } catch (LogistException e1) {
            return new AjaxResult(false, e1.getMessage());
        } catch (Exception e) {
            return new AjaxResult(false, "系统繁忙,请稍后再试");
        }

        return AjaxResult.SUCCESS;
    }

    @ResponseBody
    @RequestMapping("/userLogin")
    public Object userLogin(String username, String password, HttpServletResponse response) {
        try {
            String token = service.userLogin(username, password);
            Cookie cookie = new Cookie("token", token);
            cookie.setMaxAge(RedisKeys.VERIFY.getTime().intValue());
            cookie.setPath("/");
            response.addCookie(cookie);
        } catch (Exception e) {
            return new AjaxResult(false, e.getMessage());
        }
        return AjaxResult.SUCCESS;
    }

    //修改用户基础信息
    @RequestMapping("/userinfo/update")
    public String update(UserInfo userInfo) {
        service.update(userInfo);
        return "redirect:/login.html";
    }

    //修改用户密码
    @RequestMapping("/userInfo/updatepassword")
    @ResponseBody
    public Object updatepassword(String phone, String password, String newpassword) {
        try {
            service.updatepassword(phone, password, newpassword);
        } catch (LogistException e1) {
            return new AjaxResult(false, e1.getMessage());
        } catch (Exception e) {
            return new AjaxResult(false, "系统繁忙,请稍后再试");
        }
        return AjaxResult.SUCCESS.addData("修改成功!");
    }
}
